package com.zh.risk.controller;

import com.zh.common.annotaion.CurieCache;
import com.zh.common.entity.JSONResult;
import com.zh.risk.entity.RiskField;
import com.zh.risk.entity.RiskOperator;
import com.zh.risk.service.RulerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 规则设置控制器
 * @Author: Liard
 * @Create in: 2020/8/28 16:07
 * @Version: 1.0.1
 */
@Api(value = "规则设置控制器", tags = "RulerController")
@RestController
@RequestMapping("/rule")
@Slf4j
public class RulerController {

    @Autowired
    private RulerService service;

    @Autowired
    private RedisTemplate redisTemplate;


//    @CurieCache
    @ApiOperation(value = "获取操作符列表", tags = {"getOperatorList"}, notes = "获取操作符列表")
    @GetMapping("/getOperatorList")
    public JSONResult<List<RiskOperator>> getOperatorList(@ApiParam(value = "操作符信息实体", name = "riskOperator") RiskOperator riskOperator) {
        JSONResult<List<RiskOperator>> jsonResult = new JSONResult<>();
        try {
            List<RiskOperator> list = service.getOperatorList(riskOperator);
            jsonResult.setData(list);
            jsonResult.setSuccess(true);
            jsonResult.setMessage("查询完成!");
        } catch (Exception e) {
            log.error(e.getMessage());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

    @ApiOperation(value = "添加操作符", tags = {"addOperator"}, notes = "添加操作符")
    @PutMapping("/addOperator")
    public JSONResult<Object> addOperator(
            @ApiParam(value = "操作符信息实体", name = "riskOperator") RiskOperator riskOperator) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = service.addOperator(riskOperator);
            if (i > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setMessage("操作完成!");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("添加失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改操作符", tags = {"modOperator"}, notes = "修改操作符")
    @PutMapping("/modOperator")
    public JSONResult<Object> modOperator(
            @ApiParam(value = "操作符信息实体", name = "riskOperator") RiskOperator riskOperator) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = service.modOperator(riskOperator);
            if (i > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setMessage("操作完成!");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("修改失败!");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除操作符", tags = {"delOperator"}, notes = "删除操作符")
    @DeleteMapping("/delOperator")
    public JSONResult<Object> delOperator(
            @ApiParam(value = "操作符信息实体", name = "riskOperator") RiskOperator riskOperator) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = service.delOperator(riskOperator);
            if (i > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setMessage("操作完成!");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("删除失败!");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }



    @ApiOperation(value = "获取字段列表", tags = {"getFieldList"}, notes = "获取字段列表")
    @GetMapping("/getFieldList")
    public JSONResult<List<RiskField>> getFieldList(
            @ApiParam(value = "操作符信息实体", name = "riskField") RiskField riskField) {
        JSONResult<List<RiskField>> jsonResult = new JSONResult<>();
        try {
            List<RiskField> list = service.getFieldList(riskField);
            jsonResult.setData(list);
            jsonResult.setSuccess(true);
            jsonResult.setMessage("查询完成!");
        } catch (Exception e) {
            log.error(e.getMessage());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

//    @CurieCache
    @ApiOperation(value = "获取树形字段列表", tags = {"getFieldTreeList"}, notes = "获取树形字段列表")
    @GetMapping("/getFieldTreeList")
    public JSONResult<List<RiskField>> getFieldTreeList() {
        JSONResult<List<RiskField>> jsonResult = new JSONResult<>();
        try {
            List<RiskField> list = (List<RiskField>) redisTemplate.opsForValue().get("field_Opt");
            jsonResult.setData(list);
            jsonResult.setSuccess(true);
            jsonResult.setMessage("查询完成!");
        } catch (Exception e) {
            log.error(e.getMessage());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

    @ApiOperation(value = "添加字段", tags = {"addField"}, notes = "添加字段")
    @PutMapping("/addField")
    public JSONResult<Object> addField(
            @ApiParam(value = "操作符信息实体", name = "riskField") RiskField riskField) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = service.addField(riskField);
            if (i > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setMessage("操作完成!");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("添加失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改字段", tags = {"modField"}, notes = "修改字段")
    @PutMapping("/modField")
    public JSONResult<Object> modField(
            @ApiParam(value = "操作符信息实体", name = "riskField") RiskField riskField) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = service.modField(riskField);
            if (i > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setMessage("操作完成!");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("修改失败!");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除字段", tags = {"delField"}, notes = "删除字段")
    @DeleteMapping("/delField")
    public JSONResult<Object> delField(
            @ApiParam(value = "操作符信息实体", name = "riskField") RiskField riskField) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int i = service.delField(riskField);
            if (i > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setMessage("操作完成!");
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("删除失败!");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }


//    @CurieCache
    @ApiOperation(value = "获取聚合条件列表", tags = {"getAggList"}, notes = "获取聚合条件列表")
    @GetMapping("/getAggList")
    public JSONResult<List<HashMap<String,String>>> getAggList() {
        JSONResult<List<HashMap<String,String>>> jsonResult = new JSONResult<>();
        try {
            List<HashMap<String,String>> list = new ArrayList<>();
            HashMap<String,String> hashMap = new HashMap<>();
            HashMap<String,String> hashMap1 = new HashMap<>();
            HashMap<String,String> hashMap2 = new HashMap<>();
            HashMap<String,String> hashMap3 = new HashMap<>();
            HashMap<String,String> hashMap4 = new HashMap<>();
            hashMap.put("label", "求和");
            hashMap.put("value", "sumVal");
            list.add(hashMap);
            hashMap1.put("label", "求最大值");
            hashMap1.put("value", "maxVal");
            list.add(hashMap1);
            hashMap2.put("label", "求平均值");
            hashMap2.put("value", "avgVal");
            list.add(hashMap2);
            hashMap3.put("label", "求最小值");
            hashMap3.put("value", "minVal");
            list.add(hashMap3);
            hashMap4.put("label", "求总数");
            hashMap4.put("value", "countVal");
            list.add(hashMap4);
            jsonResult.setData(list);
            jsonResult.setSuccess(true);
            jsonResult.setMessage("查询完成!");
        } catch (Exception e) {
            log.error(e.getMessage());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
        }
        return jsonResult;
    }


}
